package com.baidumap.administrator.happystudy.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.util.SharedPreferencesHelper;
import com.ab.util.ToastUtil;
import com.baidumap.administrator.happystudy.fragment.TuiKuanShouHou;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.ui.PingJiaActivity;
import com.baidumap.administrator.happystudy.utils.GlideUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@SuppressWarnings("all")
public class FragmentOrderTuiKuanShouHouAdapter extends BaseAdapter {
    private List<Map<String, Object>> lists;
    private Activity context;
    private ViewHolder holder;
    private LayoutInflater inflater;
    private DisplayImageOptions options;
    public ArrayList<A> list;

    private SharedPreferencesHelper sHelper;

    public FragmentOrderTuiKuanShouHouAdapter(Activity _Context, SharedPreferencesHelper sHelper) {

        this.context = _Context;
        this.sHelper = sHelper;

    }

    public void bindData(List<Map<String, Object>> list) {
        this.lists = list;
    }


    class ViewHolder {

        TextView tv_shop_name;
        ImageView img_goods_img;
        TextView tv_goods_price;
        TextView tv_goods_num;
        TextView tv_total_num;
        TextView tv_total_price;
        TextView tv_goods_name;
        TextView tv_shan_chu;
        TextView tv_ping_jia;
        TextView tv_tui_kuan;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public List<Map<String, Object>> getItem(int position) {
        return (List<Map<String, Object>>) lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final int index = position;
        String shop_id = null;
        String shop_name = null;
        if (convertView != null) {

            holder = (ViewHolder) view.getTag();

        } else {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_order_wei_ping_jia, null);
            holder = new ViewHolder();

            holder.img_goods_img = (ImageView) convertView.findViewById(R.id.img_goods_img);
            holder.tv_shop_name = (TextView) convertView.findViewById(R.id.tv_shop_name);
            holder.tv_goods_price = (TextView) convertView.findViewById(R.id.tv_goods_price);
            holder.tv_goods_num = (TextView) convertView.findViewById(R.id.tv_goods_num);
            holder.tv_total_num = (TextView) convertView.findViewById(R.id.tv_total_num);
            holder.tv_total_price = (TextView) convertView.findViewById(R.id.tv_total_price);
            holder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
            holder.tv_shan_chu = (TextView) convertView.findViewById(R.id.tv_shan_chu);
            holder.tv_ping_jia = (TextView) convertView.findViewById(R.id.tv_ping_jia);
            holder.tv_tui_kuan = (TextView) convertView.findViewById(R.id.tv_tui_kuan);

            holder.tv_shan_chu.setVisibility(View.GONE);
            holder.tv_ping_jia.setVisibility(View.GONE);
            holder.tv_tui_kuan.setVisibility(View.VISIBLE);

            holder.tv_shop_name.setText((String) lists.get(position).get("shop_name"));
            holder.tv_goods_price.setText("￥" + (String) lists.get(position).get("product_price"));
            holder.tv_goods_num.setText("x" + (String) lists.get(position).get("product_num"));
            holder.tv_total_num.setText("共" + (String) lists.get(position).get("product_num") + "件商品");
            holder.tv_total_price.setText("￥" + (String) lists.get(position).get("product_price") + "(含运费：0.00)");
            holder.tv_goods_name.setText((String) lists.get(position).get("product_name"));

            GlideUtil.displayImg(context, lists.get(position).get("product_img").toString(), holder.img_goods_img);

//            final String price = (String) lists.get(position).get("qi_song_jia");
//            final String order_no = (String) lists.get(position).get("order_no");
//            final String status = (String) lists.get(position).get("status");
            holder.tv_shan_chu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    ToastUtil.showMessage(context.getActivity(), "删除订单");
//                    LoadingDialogUtil.showGifdialog(context);
//                    Api.orderDel(context, sHelper.getString(Constant.TOKEN), sHelper.getString(Constant.UID), lists.get(position).get("order_id").toString());
                }
            });
           holder.tv_tui_kuan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    ToastUtil.showMessage(context.getActivity(), "删除订单");
//                    LoadingDialogUtil.showGifdialog(context);
//                    Api.orderDel(context, sHelper.getString(Constant.TOKEN), sHelper.getString(Constant.UID), lists.get(position).get("order_id").toString());
                    Intent intent = new Intent(context, TuiKuanShouHou.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("product_id", lists.get(position).get("product_id").toString());
                    bundle.putString("order_id", lists.get(position).get("order_id").toString());
                    bundle.putString("shop_id", lists.get(position).get("shop_id").toString());

                    intent.putExtras(bundle);
                    context.startActivityForResult(intent, 200);
                }
            });
            holder.tv_ping_jia.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtil.showMessage(context, "评价");
                    Intent intent = new Intent(context, PingJiaActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("product_name", lists.get(position).get("product_name").toString());
                    bundle.putString("product_img", lists.get(position).get("product_img").toString());
                    bundle.putString("product_id", lists.get(position).get("product_id").toString());
                    bundle.putString("order_id", lists.get(position).get("order_id").toString());

                    intent.putExtras(bundle);
                    context.startActivityForResult(intent, 200);
                }
            });
            convertView.setTag(holder);
        }

        return convertView;
    }

    public class A {

        public static final int TYPE_CHECKED = 1;
        public static final int TYPE_NOCHECKED = 0;

        public boolean type;
        public String phone;

        public A(boolean type, String phone) {
            this.type = type;
            this.phone = phone;
        }
    }


}
